package cinescope2014.boundaries;

import java.sql.*;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author pascal
 */
public class JIFPaysSelect extends javax.swing.JInternalFrame {

    private Connection icn;

    /**
     * Creates new form JIFPaysSelect
     */
    public JIFPaysSelect() {
        initComponents();

        setTitle("Les Pays");

        try {
            // --- Connexion
            Class.forName("org.gjt.mm.mysql.Driver");
            String lsDSN = "jdbc:mysql://127.0.0.1:3306/cinescope2014";
            icn = DriverManager.getConnection(lsDSN, "root", "");

            Statement lstSQL = icn.createStatement();
            ResultSet lrs = lstSQL.executeQuery("SELECT * FROM pays");

            Object[] tLigne;

            DefaultTableModel ldtm = (DefaultTableModel) jTablePays.getModel();

            while (lrs.next()) {
		tLigne = new Object[5];

		tLigne[0] = lrs.getString(1);
		tLigne[1] = lrs.getString(2);
                tLigne[2] = lrs.getString(3);
                tLigne[3] = lrs.getString(4);
                tLigne[4] = lrs.getString(5);

		ldtm.addRow(tLigne);
            }

            lrs.close();

            jLabelMessage.setText("Okay !!!");

        } catch (ClassNotFoundException | SQLException ex) {
            jLabelMessage.setText(ex.getMessage());
        }

        setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTablePays = new javax.swing.JTable();
        jLabelMessage = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        jTablePays.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID pays", "Nom du pays", "Masculin", "Féminin", "Neutre"
            }
        ));
        jScrollPane1.setViewportView(jTablePays);

        jLabelMessage.setText("Message");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 385, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 241, Short.MAX_VALUE))
                    .addComponent(jScrollPane1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addComponent(jLabelMessage)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        // TODO add your handling code here:
        try {
            icn.close();
        } catch (SQLException e) {
            System.err.println(e.getMessage());
        }
    }//GEN-LAST:event_formInternalFrameClosing


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabelMessage;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTablePays;
    // End of variables declaration//GEN-END:variables
}
